<template>
  <div class="login-container flex flex_center">
    <div class="flex-direction_row login-content">
      <div class="img"></div>
      <div class="content flex_1">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left">

          <div class="flex flex_center" style="width: 100%">
            <div class="logo"/>
          </div>
          <div class="title-container" style="margin-top: 10px">
            <h3 class="title">鱼类加工管理系统</h3>
          </div>

          <el-form-item prop="username">
            <!--            <span class="svg-container">-->
            <!--              <svg-icon icon-class="user"/>-->
            <!--            </span>-->
            <span class="img-container">
              <el-image class="image" :src="require('@/assets/images/user.png')"/>
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入账号"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"/>
          </el-form-item>

          <el-form-item prop="password" style="margin-top: 30px">
            <span class="img-container">
<!--              <svg-icon icon-class="password"/>-->
              <el-image class="image" :src="require('@/assets/images/lock.png')"/>
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
          </el-form-item>

          <el-form-item prop="code" style="margin-top: 30px">
            <span class="img-container">
<!--              <svg-icon icon-class="password"/>-->
              <el-image class="image" :src="require('@/assets/images/ic_certificate.png')"/>
            </span>
            <el-input
              ref="code"
              v-model="loginForm.code"
              @keyup.enter.native="handleLogin"
              placeholder="验证码"
            />
            <!--        <span class="yzm" @click="getCodeFn">{{ code }}</span>-->
            <!--        <img class="yzm" @click="getCodeFn" src="data:image/png;base64,***************************************************"/>-->
            <img class="yzm" @click="getCodeFn" :src="code"/>
          </el-form-item>

          <el-button
            :loading="loading"
            type="primary"
            style="width:100%;margin-top: 97px"
            size="large"
            @click.native.prevent="handleLogin">登录
          </el-button>

        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { getCode } from '@/api/user'
import { createRandomData } from '@/utils/index'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length == 0) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      randomCookie: 0,
      loginForm: {},
      code: '',
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    const random = createRandomData(1, 50000)
    this.randomCookie = random
    this.getCodeFn()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    getCodeFn() { // 获取验证码
      getCode({ cookie: this.randomCookie }).then(response => {
        if (response.rCode == '200') {
          this.code = 'data:image/png;base64,' + response.resultData.code
          this.randomCookie = response.resultData.sessionId
        }
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const params = {
            username: this.loginForm.username,
            password: this.loginForm.password,
            code: this.loginForm.code,
            sessionId: this.randomCookie
          }
          this.$store.dispatch('user/login', params).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('登录失败!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$light_gray: #fff;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;
    background-color: white;
    position: relative;

    input {
      //background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $cursor;
      height: 100%;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px white inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    //border: 1px solid rgba(255, 255, 255, 0.1);
    border: 1px solid #CDCCCB;
    //background: rgba(0, 0, 0, 0.1);
    background: white;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
@import '@/styles/variables.scss';

$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  // width: 100%;
  // background-image: url("../../assets/images/bg_login.png");
  background: #F5F6F8;
  overflow: hidden;

  .login-content {
    width: 1300px;
    height: 752px;
    position: relative;

    .img {
      height: 100%;
      width: 831px;
      border-top-left-radius: 30px;
      border-bottom-left-radius: 30px;
      background-image: url("../../assets/images/bg_login_content.png");
    }

    .content {
      background-color: white;
      width: 0px;
      height: 100%;
      border-top-right-radius: 30px;
      border-bottom-right-radius: 30px;

      .logo {
        width: 286px;
        height: 118px;
        background-image: url("../../assets/images/icon_login_logo.png");
        background-repeat: no-repeat;
        background-size: 100% 100%;
      }
    }
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 57px 65px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    background-color: red;
    color: $dark_gray;
    vertical-align: middle;
    width: 33px;
    display: inline-block;
  }

  .img-container {
    display: inline-block;
    color: $dark_gray;
    vertical-align: middle;
    width: 33px;
    margin-left: 7px;
    position: relative;

    .image {
      width: 26px;
      height: 26px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }

  .yzm {
    width: 60px;
    height: 38px;
    cursor: pointer;
    position: absolute;
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
    right: 1px;
    top: 1px;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 36px;
      color: $titleTextColor;
      margin: 0px auto 36px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
